package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Longs;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.CategoryEntity;
import com.chushouya.product.dao.mapper.CategoryEntityMapper;
import com.chushouya.product.dto.admin.category.CategoryQuery;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;


@Repository
public class CategoryRepository extends BaseRepository<CategoryEntityMapper, CategoryEntity, Long> {
    public List<CategoryEntity> selectList(CategoryQuery query) {
        Example example = this.buildExample(query);
        return this.selectByExample(example);
    }

    private Example buildExample(CategoryQuery query) {
        Example example = new Example(CategoryEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Longs.notNullAndZero(query.getCategoryId())) {
            criteria.andEqualTo("categoryId", query.getCategoryId());
        }
        if (Strings.isNotEmpty(query.getCategoryName())) {
            criteria.andEqualTo("categoryName", query.getCategoryName());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        return example;
    }
}